package com.sub.project.info.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;

import com.sub.common.utils.StringUtils;
import com.sub.project.info.domain.SysProject;
import com.sub.project.info.service.ISysProjectService;
import com.sub.project.info.vo.RestVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.common.utils.poi.ExcelUtil;import com.sub.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-01-08
 */
@RestController
@RequestMapping("/system/project" )
public class SysProjectController extends BaseController {

    @Autowired
    private ISysProjectService projectService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(SysProject tbSysProject) {
        startPage();
        QueryWrapper<SysProject> queryWrapper = new QueryWrapper<SysProject>();
        if (StringUtils.isNotBlank(tbSysProject.getProjectName())){
            queryWrapper.like("project_name" ,tbSysProject.getProjectName());
        }
        if(StringUtils.isNotBlank(tbSysProject.getEnvironmental())){
            queryWrapper.eq("environmental" ,tbSysProject.getEnvironmental());
        }
        if(tbSysProject.getStartTime() != null){
            queryWrapper.eq("start_time" ,tbSysProject.getStartTime());
        }
        if(tbSysProject.getEndTime() != null){
            queryWrapper.eq("end_time" ,tbSysProject.getEndTime());
        }
        if(StringUtils.isNotBlank(tbSysProject.getDescrib())){
            queryWrapper.eq("describ" ,tbSysProject.getDescrib());
        }
        if(StringUtils.isNotBlank(tbSysProject.getModel())){
            queryWrapper.eq("model" ,tbSysProject.getModel());
        }
        if(StringUtils.isNotBlank(tbSysProject.getIntroduce())){
            queryWrapper.eq("introduce" ,tbSysProject.getIntroduce());
        }
        if(StringUtils.isNotBlank(tbSysProject.getUserId())){
            queryWrapper.eq("user_id" ,tbSysProject.getUserId());
        }
        if(tbSysProject.getCreateByUserId() != null){
            queryWrapper.eq("create_by_user_id" ,tbSysProject.getCreateByUserId());
        }
        if(tbSysProject.getUpdateByUserId() != null){
            queryWrapper.eq("update_by_user_id" ,tbSysProject.getUpdateByUserId());
        }
        List<SysProject> list = projectService.list(queryWrapper);
        return getDataTable(list);
    }

    //    @PreAuthorize("@ss.hasPermi('system:honor:list')" )
    @GetMapping("/appletList" )
    public TableDataInfo appletList( String openId) {
        List<SysProject> list = projectService.findByopenId(openId);
        Iterator iterator = list.iterator();
        List<RestVo> restVos = new ArrayList<>();
        while (iterator.hasNext()){
            SysProject honor1 =  (SysProject)iterator.next();
            RestVo restVo = (RestVo)honor1.clone();
            restVos.add(restVo);
        }
        return getDataTable(restVos);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:project:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(projectService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:project:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProject tbSysProject) {
        return toAjax(projectService.save(tbSysProject) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:project:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProject tbSysProject) {
        return toAjax(projectService.updateById(tbSysProject) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:project:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
